const Redis = require("@/utils/redis");
const { KEYS } = require("@/server/z_enter");
const PageModal = require("@/model/Page");
const z_base_Dao = require("@/dao/z_base");
const { ErrorCode } = require("@/utils/r/code");
const { cloneDeep } = require("lodash");

const page = {
  async GetList() {
    //先从redis中获取信息
    let list = await Redis.GetResult(KEYS.KEY_PAGE);
    if (!list) {
      list = await z_base_Dao.List(PageModal.Page);
    }
    return list;
  },
  async SaveOrUpdate(body) {
    const exist = await z_base_Dao.GetOne(PageModal.Page, { name: body.name });
    //判断标签名是否存在
    if (exist && body?.id !== exist?.id) {
      return ErrorCode.ERROR_PAGE_NAME_EXIST;
    }
    const page = cloneDeep(body);
    page.updated_at = new Date().toLocaleString();
    if (page.id) {
      //更新
      await z_base_Dao.Update(PageModal.Page, page, { id: page.id });
    } else {
      page.created_at = new Date().toLocaleString();
      await z_base_Dao.Create(PageModal.Page, page);
    }
    await Redis.Del(KEYS.KEY_PAGE);
    return ErrorCode.OK;
  },
  async Delete(ids) {
    await z_base_Dao.Delete(PageModal.Page, { id: ids });
    await Redis.Del(KEYS.KEY_PAGE);
    return ErrorCode.OK;
  },
};
module.exports = page;
